import http from '@/utils/request';

interface ShareReceiveParams {
    shareId: string | number;
}

interface WechatUser {
    id: string;
    openId: string;
    nickname: string;
    avatar: string;
    createTime: string;
    updateTime: string;
}

interface ShareReceiveItem {
    id: string;
    shareId: number;
    receiveUserId: string;
    createTime: string;
    wechatUser: WechatUser;
}

interface ListShareMyGiftBooksParams {
    pageNum: number;
    pageSize: number;
    searchValue?: string;
}

interface Book {
    id: string;
    title: string;
    type: string;
    coverImageUrl: string;
    userId: string;
    guestCount: number | null;
    totalAmount: number | null;
    createTime: string;
    updateTime: string;
}

export interface ShareRecord extends ShareReceiveItem {
    bookId: number;
    books: Book;
}

/**
 * 接收分享的礼簿
 * @param params 包含 shareId 的参数对象
 * @returns Promise
 */
export const receiveShare = (params: ShareReceiveParams) => {
    // 从本地存储获取token作为receiveUserId
    const token = uni.getStorageSync('token');

    return http.post('/system/shareReceive', {
        shareId: params.shareId,
        receiveUserId: token
    })
}

/**
 * 获取分享接收列表
 * @param shareId 分享ID
 * @returns Promise
 */
export const getShareReceiveList = (shareId: string | number) => {
    return http.get(`/system/shareReceive/listByShareId/${shareId}`)
}

/**
 * 取消某人的分享
 * @param id 分享接收记录ID
 * @returns Promise
 */
export const cancelShareReceive = (id: string) => {
    return http.delete(`/system/shareReceive?id=${id}`);
}

/**
 * 获取分享给我的礼簿列表
 * @param params 分页和搜索参数
 * @returns Promise
 */
export const listShareMyGiftBooks = (params: ListShareMyGiftBooksParams) => {
    return http.post('/system/shareReceive/listShareMyGiftBooks', params);
}

